#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <time.h>
#include <limits.h>

void print_bin(int);
char * itobs(int, char *);
void show_bstr(const char *);

#define LEN 81
#define SWAP(type, a, b)    do {type c=a; a=b; b=c;} while(0)

static size_t INT_BIT_LEN = CHAR_BIT * sizeof(int);

int invert_end(int, int);

int main(void)
{
    char bin_str[INT_BIT_LEN + 1];

    int number;

    puts("Enter integers and see them in binary.");
    puts("Non-numeric input terminates program.");

    while (scanf("%d", &number) == 1) {
        itobs(number, bin_str);

        show_bstr(bin_str);

        putchar('\n');

        number = invert_end(number, 4);
        itobs(number, bin_str);
        show_bstr(bin_str);
        putchar('\n');
    }

    puts("Bye!");

    return 0;
}

int invert_end(int digit, int end)
{
    for (--end; end >= 0; --end)
        digit ^= (01 << end);

    return digit;
}

char * itobs(int n, char * sav)
{
    int i;
    for (i=INT_BIT_LEN - 1; i>=0; --i, n >>= 1)
        sav[i] = '0' + (01 & n);

    sav[INT_BIT_LEN] = 0;

    return sav;
}

void show_bstr(const char * str)
{
    int i =0;
    while (*str)
    {
        putchar(*str++);
        if (++i % 4 == 0 && *str)
            putchar(' ');
    }
}



void print_bin(int n)
{
    int m = n % 2;
    if (n >= 2)
        print_bin(n / 2);

    printf("%c", m == 0? '0': '1');
}

char * s_gets(char * str, int n)
{
    char * ret_val;
    char * find;

    if ((ret_val = fgets(str, n, stdin))) {
        find = strchr(str, '\n');
        if (find)
            *find = 0;
        else
            while (getchar() != '\n')
                ;
    }

    return ret_val;
}
